package com.example.dilidili_ting.utils;

import android.app.Activity;

import com.example.dilidili_ting.adapter.LikeAdapterTing;
import com.example.dilidili_ting.bean.DataBeanTing;
import com.example.dilidili_ting.bean.LikeBeanTing;
import com.example.dilidili_ting.bean.MovieBeanTing;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.ryan.rv_gallery.AnimManager;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class LikeUtilsTing {

    public List<MovieBeanTing> listMovieTing;

    public void getLikelist(Activity mContextTing){
        JsonObject jo = new JsonObject();
        jo.addProperty("userId", SharedPreferencesUtilsTing.readLoginUserName(mContextTing));
        OkhttpUtilsTing.isPOST("favorite/getFavoriteByUserId", true, jo, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {}

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res = response.body().string();
                Gson gson = new Gson();
                final DataBeanTing<List<LikeBeanTing>> r = gson.fromJson(res, new TypeToken<DataBeanTing<List<LikeBeanTing>>>(){}.getType());
//                System.out.println(res);
                listMovieTing = new ArrayList<>();

                mContextTing.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i=0;i<r.data.size();i++){
                            getMovieId( mContextTing,r.data.get(i).getMovieId());
                        }
                    }
                });
            }
        });
    }

    private void getMovieId(Activity mContextTing,Integer id){
//        System.out.println("ii"+id);
        JsonObject jo = new JsonObject();
        jo.addProperty("movieId",id);
        OkhttpUtilsTing.isPOST("movie/getMovieById", true, jo, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {}

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res = response.body().string();
                Gson gson = new Gson();
                final DataBeanTing<MovieBeanTing> r = gson.fromJson(res, new TypeToken<DataBeanTing<MovieBeanTing>>(){}.getType());
                mContextTing.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        listMovieTing.add(r.data);
                        System.out.println(new Gson().toJson(listMovieTing));
                    }
                });
            }
        });
    }

}
